<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <h1 style="padding-left:70px;">Create Shop voucher</h1>
      <el-form-item label="shopname" prop="shopname">
        <el-input v-model="form.shopname" placeholder="shopname" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="region" prop="region">
        <el-radio v-model="form.region" label="sg">sg</el-radio>
        <el-radio v-model="form.region" label="tw">tw</el-radio>
        <el-radio v-model="form.region" label="ph">ph</el-radio>
        <el-radio v-model="form.region" label="vn">vn</el-radio>
        <el-radio v-model="form.region" label="id">id</el-radio>
        <el-radio v-model="form.region" label="my">my</el-radio>
        <el-radio v-model="form.region" label="th">th</el-radio>
        <el-radio v-model="form.region" label="br">br</el-radio>
        <el-radio v-model="form.region" label="br">br</el-radio>
        <el-radio v-model="form.region" label="co">co</el-radio>
        <el-radio v-model="form.region" label="cl">cl</el-radio>
        <el-radio v-model="form.region" label="mx">mx</el-radio>
        <el-radio v-model="form.region" label="in">in</el-radio>
      </el-form-item>
      <el-form-item label="env" prop="env">
        <el-radio v-model="form.env" label="test">test</el-radio>
        <el-radio v-model="form.env" label="uat">uat</el-radio>
        <el-radio v-model="form.env" label="staging">staging</el-radio>
      </el-form-item>
      <el-form-item style="margin-top: 20px">
        <el-button type="primary" @click="onSubmit('form')">Create Now</el-button>
      </el-form-item>
    </el-form>
    <div v-if="showResultCard" v-loading="isLoadingResult">
      <result-pane :resultReturn="resultReturn"></result-pane>
    </div>
    <br>
  </div>
</template>

<script>
import ResultPane from './ResultPane'
import {api, postRequest} from "@/api/commit";

export default {
  name: "CreateShopVoucher",
  data() {
    return {
      showResultCard: false,
      isLoadingResult: false,
      form: {
        shopname: '',
        region: 'sg',
        env: 'test'
      },
      rules: {
        shopname: [
          {required: true, message: 'shopname不能为空', trigger: 'blur'}
        ],
        region: [
          {required: true, message: 'region不能为空', trigger: 'blur'}
        ],
        env: [
          {required: true, message: 'env不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  components: {
    ResultPane
  },
  methods: {
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (!valid) {
          this.$message({
            "message": "请检查表单！",
            "type": "warning"
          })
          return
        }
        this.resultReturn = ''
        this.showResultCard = true
        this.isLoadingResult = true
        let params = Object.assign({}, this.form);
        postRequest(api['create_shop_voucher'], params).then((res) => {
          this.resultReturn = res.resultReturn
          this.isLoadingResult = false
        }, (error) => {
          this.resultReturn = "请检查您的输入是不是有问题&依赖方报错俺也没办法"
          this.isLoadingResult = false
        })
      })
    }
  },
}
</script>

<style scoped>

</style>
